package chat;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import users.User;




/**
 * The Model for a Chat
 * @author Lois Fozzard
 *
 */
public class ChatModel extends Observable{
	
    private User owner;
	protected List<Message> allMessages;
	protected List<ActionMessage> actionMessages;
	protected List<UserMessage> userMessages;
	
	/**
	 * Initializes the ChatModel
	 */
	public ChatModel(User owner){
	    this.owner = owner;
		allMessages = new ArrayList<Message>();
		actionMessages = new ArrayList<ActionMessage>();
		userMessages = new ArrayList<UserMessage>();
	}
	
	public void sendMessage(Message message)
	{
        this.setChanged();
        this.notifyObservers(message);
	}
	
	/**
	 * Adds the given Message
	 * @param message the Message to add
	 */
	public void addMessage(Message message){
		allMessages.add(message);
		if(message instanceof ActionMessage)
			actionMessages.add((ActionMessage)message);
		else if(message instanceof UserMessage)
			userMessages.add((UserMessage)message);
		this.setChanged();
		this.notifyObservers(ChatPanel.class);
	}
	
	public void addUserMessageIfNotOwner(UserMessage message)
	{
	    if(!message.getUser().equals(owner))
	    {
	        addMessage(message);
	    }
	}
	
	public List<Message> messages()
	{
		return allMessages;
	}

    public User getOwner()
    {
        return owner;
    }

}
